package com.ruoyi.api.controller;

import com.ruoyi.api.service.VimGroupApiService;
import com.ruoyi.api.vo.CurrentGroup;
import com.ruoyi.api.vo.Group;
import com.ruoyi.api.vo.UnGroup;
import com.ruoyi.api.vo.UnMessage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tio.core.ChannelContext;
import org.tio.core.Tio;
import org.tio.utils.lock.SetWithLock;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/sys/groups")

public class GroupController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(GroupController.class);

    @Resource
    private VimGroupApiService vimGroupApiService;


    /**
     * 新建群
     *
     * @param group 群
     * @return boolean
     */
    @PostMapping("")
    public AjaxResult save(@RequestBody Group group)
    {
        return AjaxResult.success(vimGroupApiService.save(group));
    }

    /**
     * 更新群
     *
     * @param group 群
     * @return boolean
     */
    @PatchMapping(value = "{id}")
    public AjaxResult update(@PathVariable String id, @RequestBody Group group) {
        group.setId(id);
        return toAjax(vimGroupApiService.update(group));
    }

    /**
     * 获取群
     *
     * @param id 群id
     * @return boolean
     */
    @GetMapping(value = "{id}")
    public AjaxResult get(@PathVariable("id") String id) {
        return AjaxResult.success(vimGroupApiService.get(id));
    }

    /**
     * 查询当前用户所在的群
     *
     * @return List
     */
    @GetMapping
    public AjaxResult list() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        return AjaxResult.success(vimGroupApiService.getGroups(String.valueOf(user.getUserId())));
    }

    /**
     * 查询当前群的所有的用户
     *
     * @return List
     */
    @GetMapping("{id}/users")
    public AjaxResult users(@PathVariable("id") String id) {
        return AjaxResult.success(vimGroupApiService.getUsers(id));
    }

    /**
     * 添加群成员
     *
     * @return List
     */
    @PostMapping("{id}/users")
    public AjaxResult addUsers(@PathVariable("id") String id,@RequestBody String[] userIds) {
        boolean a = vimGroupApiService.addUsers(id, userIds);
        if(a){
           return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("用户添加失败");
    }

    /**
     * 删除群成员
     *
     * @return List
     */
    @DeleteMapping("{id}/users")
    public AjaxResult deleteUser(@PathVariable("id") String id, @RequestBody Map<String,String[]> map) {
        if(map != null){
            String[] ids = map.get("userId");
            if(ids != null){
                return toAjax(vimGroupApiService.delUsers(id, ids));
            }
        }
        return AjaxResult.error("删除失败!");
    }

    /**
     * 删除群成员
     *
     * @return List
     */
    @DeleteMapping("{id}/users/{userId}")
    public AjaxResult deleteUser(@PathVariable("id") String id, @PathVariable("userId")String userId) {
        String[] ids = new String[]{userId};
            if(ids != null){
                return toAjax(vimGroupApiService.delUsers(id, ids));
        }
        return AjaxResult.error("删除失败!");
    }



    /**
     * 删一个群,只有群主能删除
     *
     * @param id 群id
     * @return boolean
     */
    @DeleteMapping(value = "{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        return  toAjax(vimGroupApiService.del(id));
    }


    /**
     * 通过群id查询群内用户的详细位置信息和外切圆
     * @param id
     * @return
     */
    @GetMapping("userLocation/{id}")
    public AjaxResult selectLocattion(@PathVariable("id")String id){
        CurrentGroup currentGroup = vimGroupApiService.selectUserLocation(id);
        return AjaxResult.success(currentGroup);
    }

    /**
     * 查询未读消息条数和最后一条消息
     * @param userId
     * @return
     */
    @PostMapping(value = "unread")
    public AjaxResult unread(@RequestBody String userId){
        List<UnGroup> messages = new ArrayList<>();
        List<Long> list = vimGroupApiService.getGroupsId(userId);
        for (Long aLong : list) {
            Group group = vimGroupApiService.get(Long.toString(aLong));
            UnGroup ungroup = vimGroupApiService.selectunread(userId,aLong);
            ungroup.setName(group.getName());
            ungroup.setAvater(ungroup.getAvater());
            messages.add(ungroup);
        }
        return AjaxResult.success(messages);
    }

}
